package com.example.programming.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.programming.common.Result;
import com.example.programming.common.tools.BaseController;
import com.example.programming.sys.entity.StuCourse;
import com.example.programming.sys.mapper.StuCourseMapper;
import com.example.programming.sys.mapper.UcenterMemberMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * <p>
 *  前端控制器 chrimer
 * </p>
 *
 * @author chrimer
 * @since 2020-09-08
 */
@RestController
@RequestMapping("/sys/stuCourse")
public class StuCourseController extends BaseController {

    @Autowired
    private StuCourseMapper stuCourseMapper;
    @Autowired
    private UcenterMemberMapper ucenterMemberMapper;


    @ApiImplicitParams({
    @ApiImplicitParam(name = "pageNum",paramType = "query",value = "当前页码",required = true),
    @ApiImplicitParam(name = "pageSize",paramType = "query",value = "每页显示记录数",required = true)
    })
    @ApiOperation(value = "分页获取StuCourse信息")
    @RequestMapping(method= RequestMethod.POST,value="/page")
    public Result page(
        Integer pageNum,
        Integer pageSize) {
        Result result = new Result();
        Page<StuCourse> page = new Page<StuCourse>(pageNum, pageSize);
        StuCourse parms = new StuCourse();
        QueryWrapper<StuCourse> warpper = new QueryWrapper<StuCourse>(parms);

        result.setData(stuCourseMapper.selectPage(page, warpper));
        return result;
    }
    @RequestMapping(method = RequestMethod.GET,value = "/getStudentCourse")
    public List<StuCourse> getStudentCourse(String id){
        List<StuCourse> list = new ArrayList<>();
        list = stuCourseMapper.selectList(new QueryWrapper<>());
        return list;
    }



    @ApiOperation(value = "保存修改StuCourse信息")
    @RequestMapping(method= RequestMethod.POST,value="/save")
    public Result save(@RequestBody StuCourse entity) {
        Result result = new Result();
        if (entity.getId() == null) {
            stuCourseMapper.insert(entity);
        } else {
            stuCourseMapper.updateById(entity);
        }
        return result;
    }
    @ApiOperation(value = "按id删除StuCourse，可以传入多个id用，隔开")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids",paramType = "query",value = "传入的id串，用，隔开",required = true)
    })
    @RequestMapping(method= RequestMethod.DELETE,value="/delete")
    public Result delete( String ids) {
        Result result = new Result();
        List<String> deleteIds = new ArrayList<String>();
        for (String id : ids.split(",")) {
            deleteIds.add(id);
        }
        stuCourseMapper.deleteBatchIds(deleteIds);
        return result;
    }
    //根据用户id得到用户所选课程信息
//    @RequestMapping(method = RequestMethod.GET,value = "stuCourse/{token}")
//    public Result stuCourse(@PathVariable String token){
//        Result result1 = new Result();
//
//        result1 = JwtUtil.validateToken(token);
//        String id =  (String) result1.getData();
//        System.out.println(id);
//
//        Result result = new Result();
//        StuCourse parms = new StuCourse();
//        QueryWrapper<StuCourse> wrapper = new QueryWrapper<StuCourse>(parms);
//        List<StuCourse> list = new ArrayList<>();
//        list = stuCourseMapper.selectList(wrapper.eq("stuid",id));
//
//        List el =  new ArrayList<>();
//        String[] courseids = new String[list.size()];
//        for(int i=0;i<list.size();i++)
//        {
//            courseids[i] = list.get(i).getCouseid();
//            el.add(i, stuCourseMapper.message(courseids[i]));
//        }
//
//        result.setData(el);
//        return result;
//    }

    @RequestMapping(method = RequestMethod.GET,value = "stuCourse/{username}")
    public Result stuCourse(@PathVariable String username){
        String id = ucenterMemberMapper.getIdByName(username);
//        result1 = JwtUtil.validateToken(token);
//        String id =  (String) result1.getData();
        System.out.println(username);
        System.out.println(id);

        Result result = new Result();
        StuCourse parms = new StuCourse();
        QueryWrapper<StuCourse> wrapper = new QueryWrapper<StuCourse>(parms);
        List<StuCourse> list = new ArrayList<>();
        list = stuCourseMapper.selectList(wrapper.eq("stuid",id));

        List el =  new ArrayList<>();
        String[] courseids = new String[list.size()];
        for(int i=0;i<list.size();i++)
        {
            courseids[i] = list.get(i).getCouseid();
            el.add(i, stuCourseMapper.message(courseids[i]));
        }

        result.setData(el);
        return result;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/addCourse")
    public Result addCourse(String courseid,String username){
        System.out.println(username+  "+" +courseid  );
        Result result = new Result();
        String id = ucenterMemberMapper.getIdByName(username);
        StuCourse stuCourse = new StuCourse();
        stuCourse.setCouseid(courseid);
        stuCourse.setStuid(id);
        stuCourse.setIsDeleted("0");
        stuCourse.setStarttime(LocalDateTime.now());
        System.out.println(id);
        if (stuCourseMapper.course(courseid,id)==null) {
            stuCourseMapper.insert(stuCourse);
            result.success("添加新课程成功");
        } else {
            result.fail("该课程已存在");
        }
        return result;
    }

    //根据该名学生选课的类型推荐所选类型课中销量最高的几门课程
    @RequestMapping(method = RequestMethod.POST,value = "recommendCourse/{username}")
    public Result recommendCourse(@PathVariable String username){

        //通过username获取用户id
        String id = ucenterMemberMapper.getIdByName(username);
        System.out.println(username);
        System.out.println(id);

        Result result = new Result();
        StuCourse parms = new StuCourse();
        QueryWrapper<StuCourse> wrapper = new QueryWrapper<StuCourse>(parms);
        List<StuCourse> list = new ArrayList<>();
        list = stuCourseMapper.selectList(wrapper.eq("stuid",id));
        System.out.println("list:"+list);
        List el =  new ArrayList<>();
        String[] courseids = new String[list.size()];
        for(int i=0;i<list.size();i++)
        {
            //通过用户id获取课程id
            courseids[i] = list.get(i).getCouseid();
            //通过课程id获取用户选择的课程的父级id
            el.add(i, stuCourseMapper.rCourse(courseids[i]));
        }
        System.out.println("courseidsLength:"+courseids.length);

        //去除el中重复的父id
        List<String> listNew = new ArrayList<String>(new TreeSet<String>(el));
        System.out.println("el:"+listNew);

        List nel = new ArrayList<>();

        //学生有选课时分别取出该类型课程中销量最好的4个课程
        if(courseids.length>0)
        {
            for(int i=0;i<listNew.size();i++)
            {
                System.out.println("test:"+stuCourseMapper.courseMessage(listNew.get(i)));
                nel.add(i,stuCourseMapper.courseMessage(listNew.get(i)));
            }
        }
        //学生没选课时取出销量最好的4个课程
        else
        {
            nel.add(0,stuCourseMapper.bCourseMessage());
        }
        System.out.println("nel:"+nel);
        result.setData(nel);
        return result;
    }

}
